package com.superhelper.superhelper_web.controller.base;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.superhelper.common.service.RedisService;
import com.superhelper.common.utils.ThreadPool;
import com.superhelper.superhelper_web.common.Consts;
import com.superhelper.superhelper_web.domain.Resp;
import com.superhelper.superhelper_web.service.AdminService;
import com.superhelper.superhelper_web.utils.DateEditor;
import com.superhelper.superhelper_web.utils.NumberEditor;


/**
 * 定义共用方法属性
 * 
 * @author xiejunbo
 *
 */
@Controller
public class AbstractController {

	protected Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired(required = true)
	protected AdminService adminService;


	@Autowired
	protected ThreadPool threadPool;

//	@Autowired
//	protected FileUploadService imageServer;

	@Autowired
	protected RedisService redisService;

	@Value("${file.domain}")
	protected String fileDomain;

//	@Autowired(required = true)
//	protected HXService hx;

	@Value("${from.admin.imid}")
	protected String imid;
	@Value("${online}")
	protected Boolean online;
	@Value("${news.detail.url}")
	protected String news_detail_url;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new DateEditor());
		binder.registerCustomEditor(int.class, new NumberEditor());
	}

	/**
	 * 返回成功的响应
	 * 
	 * @param data
	 * @return
	 */
	protected <T> Resp success(T data) {
		Resp resp = new Resp(Consts.Code.SUCCESS, null, data);
		return resp;
	}

	/**
	 * 返回失败的响应
	 * 
	 * @return
	 */
	protected <T> Resp fail(String msg) {
		Resp resp = new Resp(Consts.Code.FAIL, msg, null);
		return resp;
	}

	/**
	 * 单个文件上传
	 * 
	 * @param request
	 *            请求参数的封装类
	 * @param response
	 *            响应封装类
	 * @return 文件地址
	 * @throws Exception
	 */
//	protected String singleUpload(MultipartHttpServletRequest request,
//			HttpServletResponse response) throws Exception {
//		// 解析文件
//		MultiValueMap<String, MultipartFile> multiFileMap = request
//				.getMultiFileMap();
//		Iterator<Entry<String, List<MultipartFile>>> enrtyIterator = multiFileMap
//				.entrySet().iterator();
//
//		while (enrtyIterator.hasNext()) {
//			Entry<String, List<MultipartFile>> fileEntry = enrtyIterator.next();
//			for (MultipartFile partFile : fileEntry.getValue()) {
//				String ext = FilenameUtils.getExtension(partFile
//						.getOriginalFilename());
//				// TODO 以后需要做文件格式的限制，文件大小的限制
//				if (partFile.getSize() > 0) {
//					String fileName = imageServer.uploadFile(
//							partFile.getInputStream(), ext);
//					if (StringUtils.isNotBlank(fileName)) {
//						return fileDomain + fileName;
//					}
//				}
//			}
//		}
//		return null;
//	}
	
	/**
	 * 单个文件上传
	 * 
	 * @param request
	 *            请求参数的封装类
	 * @param response
	 *            响应封装类
	 * @return 文件地址
	 * @throws Exception
	 */
//	protected String singleFileUpload(MultipartFile file,
//			HttpServletResponse response) throws Exception {
//		// 解析文件
//
//		if (file != null && file.getSize() > 0) {
//
//			String ext = FilenameUtils.getExtension(file.getOriginalFilename());
//			// TODO 以后需要做文件格式的限制，文件大小的限制
//			String fileName = imageServer
//					.uploadFile(file.getInputStream(), ext);
//			if (StringUtils.isNotBlank(fileName)) {
//				return fileDomain + fileName;
//			}
//		}
//		return null;
//	}

}
